package com.example.administrator.listvite_sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Button openDB;
    private Button creadStutable;
    private Button insertDB;
    private SQLiteDatabase db;
    private EditText edtno;
    private EditText edtname;
    private EditText edtage;
    private Button insertData;
    private TextView readData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           findView();
           onClick();



    }
/*实现点击事件的方法*/
    private void onClick() {
        openDB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CreadOrOpenDB();
        }
    });

        creadStutable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StuTable();
            }
        });

        insertDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no=edtno.getText().toString();
                String name=edtname.getText().toString();
                int age=Integer.valueOf(edtage.getText().toString());
                insertStuDB(no,name,age);
            }
        });
      insertData.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              readDataFromDB();
          }
      });
    }


    /*绑定事件的方法*/
    private void findView() {
        openDB=findViewById(R.id.open_DB);
        creadStutable=findViewById(R.id.cread_stu);
        insertDB=findViewById(R.id.inser_DB);
        edtno=findViewById(R.id.txt_no);
        edtname=findViewById(R.id.txt_name);
        edtage=findViewById(R.id.txt_age);
        readData=findViewById(R.id.read_data);
       insertData=findViewById(R.id.insert_data);
    }
    private void readDataFromDB() {
        if (db==null){
            CreadOrOpenDB();
        }
       Cursor cursor= db.query("student",null,null,null,
                null,null,null);
       StringBuffer buffer=new StringBuffer();
       for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
           String no=cursor.getString(0);
           String name=cursor.getString(1);
           int age=cursor.getInt(2);
           buffer.append(no).append(" ").append(name)
                   .append(" ").append(age).append("\n");
       }
       readData.setText(buffer);
    }

    /*创建学生表*/
    private void StuTable() {
        if (db==null){
        CreadOrOpenDB();
    }
        String sql="create table if not exists student(stuno varchar(20),name varchar(20),age int)";
        db.execSQL(sql);
    }

    /*插入学生数据*/
    private void insertStuDB(String no,String name,int age) {
        if (db==null){
        CreadOrOpenDB();
    }
    /*第一种方法组装sql语句*//*
        String sql="insert into student values('"+no+"','"+name+"','"+age+"')";
        db.execSQL(sql);*/
        /*第二种，利用insert接口*/
        ContentValues values=new ContentValues();
        values.put("stuno",no);
        values.put("name",name);
        values.put("age",age);
        db.insert("student",null,values);
    }

    /*打开数据库*/
    private void CreadOrOpenDB() {
        String path=getFilesDir().getAbsolutePath()+File.separator+"stu.db";
        Log.e("MainActivity","path="+path);
       db= SQLiteDatabase.openOrCreateDatabase(path,null);
    }
}
